﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Models.Model;
using AnesSystem.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AnesSystem.ViewModels
{
    public class LaborRecordTimeCtrlViewModel : ViewModel
    {
        #region Variables
        private ObservableCollection<FrameworkElement> _timeList;
        private ObservableCollection<DisplayTimeItem> _disPlayList;
        private int _initMargin;

        #endregion


        #region Constructor
        public LaborRecordTimeCtrlViewModel()
        {
            _timeList = new ObservableCollection<FrameworkElement>();
            DisPlayList = new ObservableCollection<DisplayTimeItem>();
            InitData();
        }

        public ObservableCollection<FrameworkElement> TimeList
        {
            get
            {
                return _timeList;
            }

            set
            {
                _timeList = value;
            }
        }

        private List<PatientVitalSignsModel> _tempList;

        public List<PatientVitalSignsModel> TempList
        {
            get
            {
                return _tempList;
            }
            set
            {
                _tempList = value;
                RaisePropertyChanged("TempList");
            }
        }

        public int InitMargin
        {
            get
            {
                return _initMargin;
            }

            set
            {
                _initMargin = value;
            }
        }

        public ObservableCollection<DisplayTimeItem> DisPlayList
        {
            get
            {
                return _disPlayList;
            }

            set
            {
                _disPlayList = value;
            }
        }

        #endregion


        #region Properties


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods
        public void Clear()
        {
            TimeList.Clear();
            DisPlayList.Clear();

        }


        #endregion


        #region Public/ProtectedMethods



        public void Refresh()
        {
            Clear();
            InitData();
        }

        public void InitData()
        {
            List<PatientVitalSignsModel> timeItemList = DataManager.Instance.CurrentPatientLink.LaborAnalgesia.LaborVitalData.TimeList;
            DisPlayList.Clear();

            for (int i = 0; i < timeItemList.Count; i++)
            {
                bool flag = true;
                DisplayTimeItem item = new DisplayTimeItem();
                item.PatientVitalSigns = timeItemList[i];
                item.Margin = CalMargin(i);
                item.Index = i;
                if (i<=12)
                {
                    if (!(i%2==0))
                    {
                        flag = false;
                    }
                }
                if (flag)
                {
                    DisPlayList.Add(item);
                }
            }
        }

        private Thickness CalMargin(int i)
        {
            double Width = 28.4466666666667;
            Thickness margin;
            if (i == 0)
            {
                DateTime startTime = DataManager.Instance.CurrentPatientLink.LaborAnalgesia.LaborVitalData.TimeList[0].CaptureTime;
                DateTime laborRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.LaborRecordStartTime;
                DateTime oneHourEndTime = laborRecordStartTime.AddHours(1);
                int span = ((startTime - laborRecordStartTime).Minutes) / 5;
                margin = new Thickness(span * LocationUtil.RealTimeItem_Width - Width / 2 + 1, 0, 0, 0);
            }
            else
            {
                margin = new Thickness(LocationUtil.RealTimeItem_Width * 2 - Width, 0, 0, 0);
            }
            return margin;
        }

        public void TimeChange(DisplayTimeItem item)
        {
            PatientVitalSignsModel p = item.PatientVitalSigns;
            DateTime date = new DateTime();
            CusDatePickerWin win = new CusDatePickerWin();
            win.SelectedDateTime = DateTime.Now;
            win.ShowDialog();
            if (win.Result)
            {
                date = win.SelectedDateTime;
            }
            if (!date.Equals(new DateTime()))
            {
                date = date.AddSeconds(-date.Second).AddMilliseconds(-date.Millisecond);
                p.CaptureTime = date;
                item.PatientVitalSigns = p;
            }

        }



        public void SetRealTimeLocation()
        {
            ////if (Width == 0)
            ////{
            ////    Width = 41.38;
            ////    DateTime anesRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            ////    int span = (((DateTime)TimeList[0].Tag - anesRecordStartTime).Minutes) / 5;
            ////    TimeList[0].Margin = new Thickness(span * LocationUtil.RealTimeItem_Width - Width / 2 + 1, 0, 0, 0);
            ////    for (int i = 1; i < TimeList.Count; i++)
            ////    {
            ////        TimeList[i].Margin = new Thickness(LocationUtil.RealTimeItem_Width * 6 - Width, 0, 0, 0);
            ////    }
            ////}
            ////else
            ////{
            ////    DateTime anesRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.AnesRecordStartTime;
            ////    int span = (((DateTime)TimeList[0].Tag - anesRecordStartTime).Minutes) / 5;
            ////    TimeList[0].Margin = new Thickness(span * LocationUtil.RealTimeItem_Width - TimeList[0].ActualWidth / 2 + 1, 0, 0, 0);
            ////    for (int i = 1; i < TimeList.Count; i++)
            ////    {
            ////        TimeList[i].Margin = new Thickness(LocationUtil.RealTimeItem_Width * 6 - TimeList[i].ActualWidth, 0, 0, 0);
            ////    }
            ////}

            //if (Width == 0)
            //{
            //    Width = 41.38;
            //}
            //int index = (int)TimeList[0].Tag;
            //DateTime startTime = OperationDataPool.Instance.CurOperationData.VitalSignDataCacheDic[Constants.ISLABOR].VitalSignsMap["time"][index].CaptureTime;
            //DateTime laborRecordStartTime = DataManager.Instance.CurrentPatientLink.Status.LaborRecordStartTime;
            //DateTime oneHourEndTime = laborRecordStartTime.AddHours(1);
            //int span = ((startTime - laborRecordStartTime).Minutes) / 5;
            //TimeList[0].Margin = new Thickness(span * LocationUtil.RealTimeItem_Width - Width / 2 + 1, 0, 0, 0);
            //for (int i = 1; i < TimeList.Count; i++)
            //{
            //    TimeList[i].Margin = new Thickness(LocationUtil.RealTimeItem_Width * 2 - Width, 0, 0, 0);

            //}

        }

        #endregion


        #region EventHandlers


        #endregion

    }
}
